<%@ page language="java" pageEncoding="UTF-8"%>

<script type="text/javascript">
var prospectValidator;
var taskId = $.windowLocationSearch()["id"];

function getProspectData(){
	var datap = {
			id: $("#prospectId").val(),
			taskId: $("#taskId").val()			
		};
// 	$.each($("#saveProspectDiv > textarea,select,select-one,:hidden,:text"),function(idxp,elep){
	$.each(document.getElementById("prospectForm").elements,function(idxp,elep){
		if(elep.type == "input" || elep.type == "hidden" || elep.type == "text" || elep.type == "select" || elep.type == "select-one"){
			datap[elep.name] = $(elep).val();
		}else{
			try{console.info( elep.name + "_" + elep.type);}catch(ex){}
		}
	});
	
	return datap;
}

function doSaveProspect() {
	if (!prospectValidator.form()) {
		return;
	}
	
	block("#prospectForm");
	$.ajax({
		type: "POST",
		url: "prospect/save.json",
		data: getProspectData(),
		dataType: "json",
		success: saveProspectSucceedCallback,
		error:function(message){
			unblock("#prospectForm");
			$.alert("出错了，错误信息为：\n"+message);
		}
	});
}

function saveProspectSucceedCallback(data) {
	unblock("#prospectForm");
	if (!data.succeed) {
		$.notice("保存失败，原因为：\n"+data.message);
		return;
	}
	$.info("保存成功！");
	loadProspectSucceedCallback(data);
}
function loadProspectItem(){
	var id = $("#prospectId").val();
	if(id == undefined || id == ""){
		return;
	}
	
	block("#prospectForm");
	$.ajax({
		type: "GET",
		cache: false,
		url: "prospect/detail.json?id=" + id +"&taskId=" + taskId,
		dataType: "json",
		success: loadProspectSucceedCallback,
		error:function(message){
			unblock("#prospectForm");
			$.alert(message);
		}  
	});	
}

function loadProspectSucceedCallback(data){
	unblock("#prospectForm");
	if (!data.succeed) {
		$.notice(data.message);
		return;
	}
	
	if(data.data.templateIndicators){
		$("#templateNotExistsDiv").hide();
		fillProspectItem(data.data);
		prospectLoaded = true;
	}else{
		$("#templateNotExistsDiv").show();
	}
}

function fillProspectItem(item){
	var templateIndicators = item.templateIndicators;
	var html = "";
	var ignores = {
		"id" : "id",
		"task" : "task",
		"templateIndicators" : "templateIndicators",
		"cprimaryDevice01" : "cprimaryDevice01",
		"cprimaryDevice02" : "cprimaryDevice02",
		"cprimaryDevice03" : "cprimaryDevice03",
		"cprimaryDevice04" : "cprimaryDevice04",
		"cprimaryDevice05" : "cprimaryDevice05",
		"cprimaryDevice06" : "cprimaryDevice06",
		"cprimaryDevice07" : "cprimaryDevice07",
		"cprimaryDevice08" : "cprimaryDevice08",
		"cprimaryDevice09" : "cprimaryDevice09",
		"cprimaryDevice13" : "cprimaryDevice13",
		"cprimaryDevice17" : "cprimaryDevice17",
		"cprimaryDevice18" : "cprimaryDevice18",
		"cprimaryDevice19" : "cprimaryDevice19",
		"cprimaryDevice20" : "cprimaryDevice20",
		"cprimaryDevice21" : "cprimaryDevice21",
		"cprimaryDevice22" : "cprimaryDevice22",
		"cprimaryDevice26" : "cprimaryDevice26",
		"cprimaryDevice31" : "cprimaryDevice31",
		"cprimaryDevice32" : "cprimaryDevice32",
		"ltefddprimaryDevice01" : "ltefddprimaryDevice01",
		"ltefddprimaryDevice02" : "ltefddprimaryDevice02",
		"ltefddprimaryDevice04" : "ltefddprimaryDevice04",
		"ltefddprimaryDevice05" : "ltefddprimaryDevice05",
		"ltefddprimaryDevice06" : "ltefddprimaryDevice06",
		"ltefddprimaryDevice13" : "ltefddprimaryDevice13",
		"ltefddprimaryDevice14" : "ltefddprimaryDevice14",
		"ltefddprimaryDevice15" : "ltefddprimaryDevice15",
		"ltefddprimaryDevice16" : "ltefddprimaryDevice16",
		"ltefddprimaryDevice17" : "ltefddprimaryDevice17",
		"ltefddprimaryDevice23" : "ltefddprimaryDevice23",
		"ltefddprimaryDevice25" : "ltefddprimaryDevice25",
		"ltefddprimaryDevice26" : "ltefddprimaryDevice26",
		"ltetddprimaryDevice01" : "ltetddprimaryDevice01",
		"ltetddprimaryDevice02" : "ltetddprimaryDevice02",
		"ltetddprimaryDevice04" : "ltetddprimaryDevice04",
		"ltetddprimaryDevice05" : "ltetddprimaryDevice05",
		"ltetddprimaryDevice06" : "ltetddprimaryDevice06",
		"ltetddprimaryDevice13" : "ltetddprimaryDevice13",
		"ltetddprimaryDevice15" : "ltetddprimaryDevice15",
		"ltetddprimaryDevice16" : "ltetddprimaryDevice16",
		"ltetddprimaryDevice17" : "ltetddprimaryDevice17",
		"ltetddprimaryDevice22" : "ltetddprimaryDevice22",
		"ltetddprimaryDevice25" : "ltetddprimaryDevice25",
		"ltetddprimaryDevice26" : "ltetddprimaryDevice26",
		"cantennaFeed01" : "cantennaFeed01",
		"cantennaFeed02" : "cantennaFeed02",
		"cantennaFeed14" : "cantennaFeed14",
		"ltefddantennaFeed01" : "ltefddantennaFeed01",
		"ltefddantennaFeed02" : "ltefddantennaFeed02",
		"ltefddantennaFeed03" : "ltefddantennaFeed03",
		"ltefddantennaFeed14" : "ltefddantennaFeed14",
		"ltetddantennaFeed01" : "ltetddantennaFeed01",
		"ltetddantennaFeed02" : "ltetddantennaFeed02",
		"ltetddantennaFeed03" : "ltetddantennaFeed03",
		"ltetddantennaFeed14" : "ltetddantennaFeed14",
		"powerSupply01" : "powerSupply01",
		"powerSupply02" : "powerSupply02",
		"powerSupply03" : "powerSupply03",
		"powerSupply06" : "powerSupply06",
		"powerSupply07" : "powerSupply07",
		"powerSupply08" : "powerSupply08",
		"powerSupply13" : "powerSupply13",
		"powerSupply16" : "powerSupply16",
		"powerSupply24" : "powerSupply24",
		"powerSupply27" : "powerSupply27",
		"powerSupply28" : "powerSupply28",
		"powerSupply31" : "powerSupply31",
		"machineRoom01" : "machineRoom01",
		"machineRoom02" : "machineRoom02",
		"machineRoom03" : "machineRoom03",
		"machineRoom06" : "machineRoom06",
		"machineRoom07" : "machineRoom07",
		"machineRoom11" : "machineRoom11",
		"machineRoom12" : "machineRoom12",
		"machineRoom13" : "machineRoom13",
		"machineRoom14" : "machineRoom14",
		"machineRoom15" : "machineRoom15",
		"machineRoom16" : "machineRoom16",
		"structuralMast01" : "structuralMast01",
		"structuralMast02" : "structuralMast02",
		"structuralMast03" : "structuralMast03",
		"structuralMast12" : "structuralMast12"
	};
	if(templateIndicators){
		$.each(templateIndicators,function(firstIdx, first){
			html += "<fieldset style='border: 1px #CCCCCC solid;margin: 0px;padding: 3px;'>";
			html += "<legend style='width: "+first.name.length*19+"px;margin-left:20px;font-size:19px;'>"+first.name+"</legend>";
			$.each(first.children, function(secondIdx, second){
				html += "<fieldset style='border: 1px #F0F0F0 solid;margin: auto;padding: auto; width:98%;'>";
				html += "<legend style='width: "+second.name.length*13+"px;margin-left:20px;font-size:13px;'>"+second.name+"</legend>";
				$.each(second.children, function(indicatorIdx,indicator){
					var boundProperties = indicator.boundProperties;
					var boundProperties2 = boundProperties;
					if(ignores[boundProperties]){
						boundProperties2 = boundProperties+"Id";
					}
					ignores[boundProperties2] = boundProperties2;
					var value = !item[boundProperties2] ? "" : item[boundProperties2];
					if(indicator.type == "1"){
						html += buildSelectEle(boundProperties2, value, indicator.name, indicator.values);
					}else if(indicator.type == "2"){
						html += buildTextAreaEle(boundProperties2, value, indicator.name);
					}else{
						html += buildInputEle(boundProperties2, value, indicator.name);
					}
				});
				html += "</fieldset>";
			});
			html += "</fieldset>";
		});
	}

	html += "<div style='display:none'>";
	$.each(item, function(field,value){
		value = !value ? "" : value;
		if(field == "id"){
			$("#prospectId").val(value);
		}else if(!ignores[field]){
			html += buildHiddenEle(field, value);
		}
	});
	html += "</div>";
	
	$("#saveProspectDiv").html(html);
}

function buildSelectEle(name, value, indicatorName, valueList){
	var html = "";
	html+="<div class='control-group'><label for='"+name+"' class='control-label'>"+indicatorName+"：</label><div class='controls'>";
	html+="<select name='"+name+"' id='"+name+"' disabled='disabled'>";
	$.each(valueList, function(idx, val){
		html+="<option value='"+val.id+"'";
		if(val.id == value){
			html+="selected='selected'";
		}
		html+=">"+val.name+"</option>";
	});
	html+="</select></div></div>";
	return html;
}

function buildTextAreaEle(name, value, indicatorName){
	return "<div class='control-group'><label for='"+name+"' class='control-label'>"+indicatorName+"：</label><div class='controls'>"
			+"<textarea name='"+name+"' id='"+name+"' value='"+value+"' readonly='readonly'></textarea>"
			+"</div></div>";
}

function buildInputEle(name, value, indicatorName){
	return "<div class='control-group'><label for='"+name+"' class='control-label'>"+indicatorName+"：</label><div class='controls'>"
			+"<input type='text' name='"+name+"' id='"+name+"' value='"+value+"' readonly='readonly'/>"
			+"</div></div>";
}

function buildHiddenEle(name, value){
	return "<input type='text' name='"+name+"' id='"+name+"' value='"+value+"'/>";
}

$(document).ready(function() {
	$("textarea").tah({
		moreSpace:10
	});
	$().maxlength();
	
// 	loadProspectItem();
	
	prospectValidator = $("#prospectForm").validate({
		rules: {
// 			taskContentOfTask:{required:false, maxlength:1000},
// 			taskDescriptionOfTask:{required:false, maxlength:1000},
// 			constructionUnit:{required:true, maxlength:100 },
// 			constructionCase:{required:true, maxlength:1000 },
// 			constructionQuality:{required:true, maxlength:1000 },
// 			constructionSafetyCulture:{required:true, maxlength:1000 },
// 			otherSituations:{required:true, maxlength:1000 },
// 			rectificationMeasures:{required:true, maxlength:1000 }
			//,
			//recordDate:{required:true, dateISO:true}
		},
		messages:{
// 			taskContentOfTask:{required:"请输入任务描述", maxlength:"任务描述不能超过1000个字符"},
// 			taskDescriptionOfTask:{maxlength:"勘察内容不能超过1000个字符"},
// 			constructionUnit:{required:"请输入施工单位",maxlength:"施工单位不能超过1000个字符"},
// 			constructionCase:{required:"请输入施工动态",maxlength:"施工动态不能超过1000个字符"},
// 			constructionQuality:{required:"请输入施工质量",maxlength:"施工质量不能超过1000个字符"},
// 			constructionSafetyCulture:{required:"请输入安全文明施工",maxlength:"安全文明施工不能超过1000个字符"},
// 			otherSituations:{required:"请输入其他情况记录",maxlength:"其他情况记录不能超过1000个字符"},
// 			rectificationMeasures:{required:"请输入其他情况记录", maxlength:"其他情况记录不能超过1000个字符"}
			//,
			//recordDate:{required:"请输入填报时间", dateISO:"请输入有效日期，如2012-12-21"}
		}
	});

});
</script>
<div id="prospectCarousel" class="carousel slide">
<form id="prospectForm" class="form-horizontal">
<input type="hidden" id="prospectId" name="prospectId" />

<fieldset>
	<div id="saveProspectDiv">
	</div>
	<div class="well" id="templateNotExistsDiv" style="display:none">
		此任务尚未选择模板，请先选择！
	</div>
<!-- 	<div class="form-actions"> -->
<!-- 		<a id="saveBtn" href="javascript:void(0);" class="btn btn-primary" onclick="doSaveProspect()"><i class="icon-ok"></i>保存</a> -->
<!-- 	</div> -->
</fieldset>
</form>
</div>